using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
namespace MicroMsg.Manager
{
	public class ChatTimeManager
	{
		public static void AddMsgListToTop(MMObservableCollection<ChatMsg, ChatListItem> talklist, List<ChatMsg> toAddList)
		{
			if (talklist == null)
			{
				return;
			}
			if (toAddList == null)
			{
				return;
			}
			if (toAddList.get_Count() <= 0)
			{
				return;
			}
			ChatTimeMsg chatTimeItem = ChatTimeManager.getChatTimeItem(toAddList.get_Item(0));
			ChatTimeMsg topMsg = chatTimeItem;
			talklist.Insert(0, chatTimeItem);
			int num = 0 + 1;
			talklist.Insert(1, toAddList.get_Item(0));
			num = 1 + 1;
			for (int i = 1; i < toAddList.get_Count(); i++)
			{
				ChatMsg chatMsg = toAddList.get_Item(i);
				if (ChatTimeManager.isNeedAddTime(topMsg, chatMsg))
				{
					ChatTimeMsg chatTimeItem2 = ChatTimeManager.getChatTimeItem(chatMsg);
					talklist.Insert(num, chatTimeItem2);
					num++;
					talklist.Insert(num, chatMsg);
					num++;
					topMsg = chatTimeItem2;
				}
				else
				{
					talklist.Insert(num, chatMsg);
					num++;
				}
			}
		}
		public static void FillTimeMsg(MMObservableCollection<ChatMsg, ChatListItem> talklist)
		{
			if (talklist == null || talklist.get_Count() == 0)
			{
				return;
			}
			if (talklist[0] is ChatTimeMsg)
			{
				return;
			}
			ChatTimeMsg chatTimeItem = ChatTimeManager.getChatTimeItem(talklist[0]);
			talklist.Insert(0, chatTimeItem);
		}
		public static void AddMsgToBottom(MMObservableCollection<ChatMsg, ChatListItem> talklist, ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null)
			{
				return;
			}
			if (talklist == null)
			{
				return;
			}
			if (talklist.get_Count() <= 0)
			{
				ChatTimeMsg chatTimeItem = ChatTimeManager.getChatTimeItem(chatMsg_0);
				talklist.Add(chatTimeItem);
				talklist.Add(chatMsg_0);
				return;
			}
			ChatTimeMsg chatTimeMsg = null;
			for (int i = talklist.get_Count() - 1; i >= 0; i--)
			{
				ChatMsg chatMsg = talklist[i];
				if (chatMsg is ChatTimeMsg)
				{
					chatTimeMsg = (chatMsg as ChatTimeMsg);
					IL_5D:
					if (chatTimeMsg == null)
					{
						ChatTimeMsg chatTimeItem2 = ChatTimeManager.getChatTimeItem(chatMsg_0);
						talklist.Add(chatTimeItem2);
						talklist.Add(chatMsg_0);
					}
					else
					{
						if (ChatTimeManager.isNeedAddTime(chatTimeMsg, chatMsg_0))
						{
							ChatTimeMsg chatTimeItem3 = ChatTimeManager.getChatTimeItem(chatMsg_0);
							talklist.Add(chatTimeItem3);
							talklist.Add(chatMsg_0);
						}
						else
						{
							talklist.Add(chatMsg_0);
						}
					}
					if (!chatMsg_0.IsSender() && chatMsg_0.IsTxt())
					{
						List<EggItem> containChatEggs = ChatContainerPage.getContainChatEggs(chatMsg_0.strContent);
						if (containChatEggs.get_Count() > 0)
						{
							ChatContainerPage.doChatEggs(containChatEggs, ChatEggsCtrl.EggType.ReceiveEgg);
						}
					}
					return;
				}
			}
			goto IL_5D;
		}
		public static bool isNeedAddTime(ChatMsg topMsg, ChatMsg bottomMsg)
		{
			if (topMsg != null && bottomMsg != null)
			{
				long num = Math.Abs(topMsg.nCreateTime - bottomMsg.nCreateTime);
				return num >= 300L;
			}
			return false;
		}
		public static ChatTimeMsg getChatTimeItem(ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null)
			{
				return null;
			}
			return new ChatTimeMsg
			{
				nCreateTime = chatMsg_0.nCreateTime
			};
		}
		public static int GetRealMsgCount(MMObservableCollection<ChatMsg, ChatListItem> talklist)
		{
			if (talklist != null && talklist.get_Count() != 0)
			{
				int count = talklist.get_Count();
				int num = 0;
				for (int i = 0; i < talklist.get_Count(); i++)
				{
					ChatMsg chatMsg = talklist[i];
					if (chatMsg != null && chatMsg is ChatTimeMsg)
					{
						num++;
					}
				}
				return count - num;
			}
			return 0;
		}
		public static void RemoveMsgAt(MMObservableCollection<ChatMsg, ChatListItem> talklist, int index)
		{
			if (talklist == null || talklist.get_Count() <= 0)
			{
				return;
			}
			if (index >= talklist.get_Count())
			{
				return;
			}
			ChatMsg chatMsg = talklist[index];
			if (chatMsg is ChatTimeMsg)
			{
				return;
			}
			if (index == 0)
			{
				talklist.RemoveAt(index);
				return;
			}
			ChatMsg chatMsg2 = talklist[index - 1];
			if (!(chatMsg2 is ChatTimeMsg))
			{
				talklist.RemoveAt(index);
				return;
			}
			bool flag = false;
			if (index == talklist.get_Count() - 1)
			{
				flag = true;
			}
			else
			{
				ChatMsg chatMsg3 = talklist[index + 1];
				if (chatMsg3 is ChatTimeMsg)
				{
					flag = true;
				}
			}
			if (flag)
			{
				talklist.RemoveAt(index);
				talklist.RemoveAt(index - 1);
				return;
			}
			talklist.RemoveAt(index);
		}
	}
}
